# encoding: utf-8

from PyQt5.Qt import *
from widget.my_window import MyWindow


class InfoBox(MyWindow):
    '''
    提示框
    '''
    def __init__(self, info):
        super().__init__()
        self.setWindowModality(Qt.ApplicationModal)
        self.my_info = info
        self.resize(300, 250)
        self.label_title = QLabel(self)
        self.label_title.resize(230, 40)
        self.label_title.setText('info')
        self.label_title.move(10, 5)
        # label_title.setStyleSheet("font-size: 14px;")
        self.label_info = QLabel(self)
        self.label_info.setAlignment(Qt.AlignCenter)
        self.label_info.setWordWrap(True)
        self.label_info.setText(self.my_info)
        self.label_info.resize(280, 180)
        self.label_info.move(10, 45)
        self.set_up_bottom_btn()

    def set_up_bottom_btn(self):
        '''
        初始化底部按钮
        :return:
        '''
        self.cancel_btn = QPushButton(parent=self, text='确定')
        self.cancel_btn.clicked.connect(self.close)
        self.cancel_btn.move(self.width() - 110, self.height() - 50)

    def keyPressEvent(self, QKeyEvent):  # 键盘某个键被按下时调用
        '''
        按下 ESC关闭当前页
        :param QKeyEvent:
        :return:
        '''
        # 参数1  控件
        if QKeyEvent.key() == Qt.Key_Escape:  # 判断是否按下了ESC键
            self.close_btn.click()


class ChoiceBox(MyWindow):
    '''
    选择框
    '''
    def __init__(self, info):
        super().__init__()
        self.setWindowModality(Qt.ApplicationModal)
        self.my_info = info
        self.resize(300, 250)
        self.label_title = QLabel(self)
        self.label_title.resize(230, 40)
        self.label_title.setText('info')
        self.label_title.move(10, 5)
        # label_title.setStyleSheet("font-size: 14px;")
        self.label_info = QLabel(self)
        self.label_info.setAlignment(Qt.AlignCenter)
        self.label_info.setWordWrap(True)
        self.label_info.setText(self.my_info)
        self.label_info.resize(280, 180)
        self.label_info.move(10, 45)
        self.set_up_bottom_btn()

    def set_up_bottom_btn(self):
        '''
        初始化底部按钮
        :return:
        '''
        self.enter_btn = QPushButton(parent=self, text='确定')
        self.enter_btn.move(120, 180)
        self.cancel_btn = QPushButton(parent=self, text='取消')
        self.cancel_btn.clicked.connect(self.close)
        self.cancel_btn.move(200, 180)

    def keyPressEvent(self, QKeyEvent):  # 键盘某个键被按下时调用
        '''
        按下 ESC关闭当前页
        :param QKeyEvent:
        :return:
        '''
        # 参数1  控件
        if QKeyEvent.key() == Qt.Key_Escape:  # 判断是否按下了ESC键
            self.close_btn.click()


class CustomBox(MyWindow):
    '''
    自定义框
    '''
    def __init__(self, title, width=300, height=250):
        super().__init__()
        self.setWindowModality(Qt.ApplicationModal)
        # self.my_info = info
        self.setFixedSize(width, height)
        # self.resize(300, 250)
        self.label_title = QLabel(self)
        self.label_title.resize(230, 40)
        self.label_title.setText(title)
        self.label_title.move(10, 5)
        # label_title.setStyleSheet("font-size: 14px;")
        # self.label_info = QLabel(self)
        # self.label_info.setAlignment(Qt.AlignCenter)
        # self.label_info.setWordWrap(True)
        # self.label_info.setText(self.my_info)
        # self.label_info.resize(280, 180)
        # self.label_info.move(10, 45)
        self.set_up_bottom_btn()

    def set_up_bottom_btn(self):
        '''
        初始化底部按钮
        :return:
        '''
        self.enter_btn = QPushButton(parent=self, text='确定')
        self.enter_btn.move(self.width() - 200, self.height() - 50)
        self.cancel_btn = QPushButton(parent=self, text='取消')
        self.cancel_btn.clicked.connect(self.close)
        self.cancel_btn.move(self.width() - 110, self.height() - 50)

    def keyPressEvent(self, QKeyEvent):  # 键盘某个键被按下时调用
        '''
        按下 ESC关闭当前页
        :param QKeyEvent:
        :return:
        '''
        # 参数1  控件
        if QKeyEvent.key() == Qt.Key_Escape:  # 判断是否按下了ESC键
            self.close_btn.click()


class ProgressBox(MyWindow):
    '''
    进度条框
    '''
    def __init__(self, sum_num):
        super().__init__()
        self.setWindowModality(Qt.ApplicationModal)
        self.sum_num = sum_num
        self.success_num = 0
        self.fail_num = 0
        self.resize(300, 250)
        self.label_title = QLabel(self)
        self.label_title.resize(230, 40)
        self.label_title.setText('info')
        self.label_title.move(10, 5)
        # label_title.setStyleSheet("font-size: 14px;")

        # 提示信息
        self.label_info1 = QLabel(self)
        self.label_info1.setAlignment(Qt.AlignCenter)
        self.label_info1.setWordWrap(True)
        self.label_info1.setText('开始导入...')
        self.label_info1.resize(280, 80)
        self.label_info1.move(10, 45)

        # 进度条
        self.pbar = QProgressBar(self)
        self.pbar.resize(260, 20)
        self.pbar.move(20, 120)
        self.pbar.setValue(0)  # 初始化进度为0

        # 确定按钮
        self.enter_btn = QPushButton(self)
        self.enter_btn.resize(50, 25)
        self.enter_btn.setText("确定")
        self.enter_btn.move(200, 160)
        self.enter_btn.clicked.connect(self.close_btn.click)
        self.enter_btn.close()

        # 取消按钮
        self.cancel_btn = QPushButton(self)
        self.cancel_btn.resize(50, 25)
        self.cancel_btn.setText("取消")
        self.cancel_btn.move(200, 160)

# 信号槽相关（重点）知识 start ----------------------------------------------
    # 更新进度条的槽函数
    def update_progress(self, success_num, fail_num, cur_num):
        '''
        更新进度条的接收槽
        :param success_num:
        :param fail_num:
        :return:
        '''
        # self.success_num += success_num
        self.success_num = success_num
        # self.fail_num += fail_num
        self.fail_num = fail_num
        self.label_info1.setText('成功 {0} 条， 失败 {1} 条'.format(self.success_num, self.fail_num))
        step = int(cur_num / self.sum_num * 100)
        print('success_num: {0}-{1}'.format(self.success_num, type(self.success_num)))
        print('fail_num: {0}-{1}'.format(self.fail_num, type(self.fail_num)))
        print('sum_num: {0}-{1}'.format(self.sum_num, type(self.sum_num)))
        print('step: {0}-{1}'.format(step, type(step)))
        self.pbar.setValue(step)

# # 进度条的信号类（定义在对应的页面）
# class ProgressSignal(QObject):
#     '''
#     进度条自定义信号
#     '''
#     # 定义信号，信号有两个参数，两个参数的类型分别为str,str，信号名称为dataChanged
#     data_changed = pyqtSignal(int, int, name="dataChanged")
#
# # 初始化信号
# self.sig = ProgressSignal()
# # 绑定槽
# self.sig.dataChanged.connect(self.table1.parent().progress_box.update_progress)
# # 发送信号
# self.sig.dataChanged.emit(success_num, fail_num)

# 信号槽相关（重点）知识 end ----------------------------------------------

    def import_finished(self, flag):
        '''
        导入结束的接收槽
        :param flag:
        :return:
        '''
        if flag:
            self.enter_btn.show()
            self.cancel_btn.close()

